package com.zw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.dto.ProjectDto;
import com.zw.entity.Project;
import com.zw.mapper.ProjectMapper;
import com.zw.service.BudgetService;
import com.zw.service.ProjectService;
import com.zw.vo.ProjectVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Resource
    private BudgetService budgetService;

    @Override
    public List<ProjectVo> projectList(ProjectDto projectDto) {
        // 查询当前用户的 指定目录下的项目
        LambdaQueryWrapper<Project> wq = new LambdaQueryWrapper<>();
//        wq.eq(Project::getUserId, projectDto.getUserId());
        wq.eq(Project::getDirectoryId, projectDto.getDirectoryId());
        wq.orderByAsc(Project::getCreateTime);
        List<Project> projects = baseMapper.selectList(wq);
        return BeanUtil.copyToList(projects, ProjectVo.class);
    }


    @Override
    public List<Project> selectProjectList(Project project) {
        return baseMapper.selectProjectList(project);
    }

    @Override
    public Project selectProjectById(Long id) {
        List<Project> projects = baseMapper.selectProjectList(Project.builder().id(id).build());
        return CollectionUtils.isEmpty(projects) ? null : projects.get(0);
    }

    @Override
    public void removeProjectById(Long id) {
        this.removeById(id);
        budgetService.removeByProjectId(id);
    }
}
